{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pandas version:1.4.1\n",
      "numpy version:1.22.3\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "print(f'pandas version:{pd.__version__}')\n",
    "print(f'numpy version:{np.__version__}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 01 保存excel\n",
    "\n",
    "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_excel.html\n",
    "\n",
    "代码中所用的数据文件获取地址：\n",
    "\n",
    "https://url28.ctfile.com/f/23159928-560938229-153393\n",
    "（访问密码：2021）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# DataFrame.to_excel(\n",
    "#     excel_writer,\n",
    "#     sheet_name=\"Sheet1\",\n",
    "#     na_rep=\"\",\n",
    "#     float_format=None,\n",
    "#     columns=None,\n",
    "#     header=True,\n",
    "#     index=True,\n",
    "#     index_label=None,\n",
    "#     startrow=0,\n",
    "#     startcol=0,\n",
    "#     engine=None,\n",
    "#     merge_cells=True,\n",
    "#     encoding=None,\n",
    "#     inf_rep=\"inf\",\n",
    "#     verbose=True,\n",
    "#     freeze_panes=None,\n",
    "#     storage_options=None,\n",
    "# )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   a  b  c\n",
       "0  5  8  9\n",
       "1  5  0  0\n",
       "2  1  7  6\n",
       "3  9  2  4\n",
       "4  5  2  4"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# demo1\n",
    "np.random.seed(1)\n",
    "arr = np.random.randint(10,size=[5,3])\n",
    "df1 = pd.DataFrame(arr,columns=list('abc'))\n",
    "df1.to_excel('data/data02-1.xlsx')\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   a  b  c\n",
       "0  5  8  9\n",
       "1  5  0  0\n",
       "2  1  7  6\n",
       "3  9  2  4\n",
       "4  5  2  4"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# demo2,设置索引 index\n",
    "np.random.seed(1)\n",
    "arr = np.random.randint(10,size=[5,3])\n",
    "df1 = pd.DataFrame(arr,columns=list('abc'))\n",
    "df1.to_excel('data/data02-2.xlsx',index=False)\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   a  b  c\n",
       "0  5  8  9\n",
       "1  5  0  0\n",
       "2  1  7  6\n",
       "3  9  2  4\n",
       "4  5  2  4"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# demo3, 设置 sheet_name\n",
    "np.random.seed(1)\n",
    "arr = np.random.randint(10,size=[5,3])\n",
    "df1 = pd.DataFrame(arr,columns=list('abc'))\n",
    "df1.to_excel(\n",
    "    'data/data02-3.xlsx',\n",
    "    index=False,\n",
    "    sheet_name='sheet02'\n",
    "    )\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# demo4\n",
    "# 同时保存两个 dataframe 到 excel 的两个sheet 中\n",
    "\n",
    "np.random.seed(1)\n",
    "arr = np.random.randint(10,size=[5,3])\n",
    "df1 = pd.DataFrame(arr,columns=list('abc'))\n",
    "\n",
    "np.random.seed(2)\n",
    "arr = np.random.randint(10,size=[7,3])\n",
    "df2 = pd.DataFrame(arr,columns=list('abc'))\n",
    "\n",
    "with pd.ExcelWriter('data/data02-4.xlsx') as writer:  \n",
    "    df1.to_excel(writer, sheet_name='data1')\n",
    "    df2.to_excel(writer, sheet_name='data2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 往已有的excel中添加dataframe\n",
    "# 将 data02-4.xlsx 复制一份，\n",
    "# 并重新命名为 data02-5.xlsx\n",
    "np.random.seed(2)\n",
    "arr = np.random.randint(10,size=[5,4])\n",
    "df3 = pd.DataFrame(arr,columns=list('abcd'))\n",
    "\n",
    "with pd.ExcelWriter('data/data02-5.xlsx',mode='a',engine=\"openpyxl\") as writer:\n",
    "    df3.to_excel(writer, sheet_name='data3')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 02《图解Pandas》专题汇总\n",
    "\n",
    "《图解Pandas》系列已发布的图文汇总如下：\n",
    "\n",
    "- [《图解Pandas》内容框架介绍](https://mp.weixin.qq.com/s/gh063BUAM90vFhy6ZLaznw)\n",
    "- [图文01-数据结构介绍](https://mp.weixin.qq.com/s/H9kJf9zJU7ys6esr0DBhHg)\n",
    "- [图文02-创建数据对象](https://mp.weixin.qq.com/s/tH8bc20DvhA7i8HDlWv0uQ)\n",
    "\n",
    "考虑到《图解Pandas》系列内容在不断更新过程中，大家可以通过下面的专题来找到最新发布的内容。\n",
    "\n",
    "[![](https://tva1.sinaimg.cn/large/e6c9d24egy1h01f6wflmkj20go05kjrh.jpg)](https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzI2NjY5NzI0NA==&action=getalbum&album_id=2293754972943122444#wechat_redirect)\n",
    "\n",
    "同时考虑到，以后如果文章数量较多（比如超过50篇文章），可能在专题中也不好快速的找到所需要的内容，我会以文章汇总的形式，将《图解Pandas》系列的文章进行手动汇总，并形成 `图解Pandas汇总` 的专题，最新的汇总文章，可以点击下面专题，找到最新的文章即可。\n",
    "\n",
    "[![](https://tva1.sinaimg.cn/large/e6c9d24egy1h01f6yjzssj20go05kweo.jpg)](https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzI2NjY5NzI0NA==&action=getalbum&album_id=2293756873331933190#wechat_redirect)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "a8bfd21a80539ebb77057c486b7ef88ce4c7401bb1357d37b2bcc410825d60c5"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 64-bit ('manimce': conda)",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
